﻿namespace Data.Repository
{
	using System;
	using System.Linq;
	using System.Collections.Generic;

	public class CountryRepository : Data.Repository.ICountryRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region ICountryRepository Members

		/// <summary>
		/// create new country
		/// </summary>
		/// <param name="theNewCountry">the new country entity</param>
		/// <returns>the new country's ID</returns>
		public int CreateNewCountry(Data.Entity.Country theNewCountry)
		{
			if (theNewCountry == null)
			{ return 0; 
			}

			if (_dbContext == null)
			{
				_dbContext = new LinqToECommerceDBDataContext();
			}

			throw new NotImplementedException();
		}

		public IList<Data.Entity.Country> GetAllCountry()
		{
			List<ECommerce_GetAllCountryResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetAllCountry();

				lstData = data.ToList();
			}

			if(lstData != null || lstData.Count > 0)
			{
				var result = new List<Data.Entity.Country>();

				foreach(var c in lstData)
				{
					var theEntity = new Data.Entity.Country()
					{
						AllowsBilling = c.AllowsBilling,
						AllowsRegistration = c.AllowsRegistration,
						AllowsShipping = c.AllowsShipping,
						CountryID = c.CountryID,
						DisplayOrder = c.DisplayOrder,
						Name = c.Name,
						NumericISOCode = c.NumericISOCode,
						Published = c.Published,
						ThreeLetterISOCode = c.ThreeLetterISOCode,
						TwoLetterISOCode = c.TwoLetterISOCode
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		//public Data.Entity.Country GetTheCountry(int id)
		//{
		//    if(id <= 0)
		//    {
		//        return null;
		//    }

		//    if(_dbContext == null)
		//    {
		//        _dbContext = new LinqToECommerceDBDataContext();
		//    }

		//    return (from c in _dbContext.Countries
		//            where c.CountryID.Equals(id)
		//            select new Data.Entity.Country
		//            {
		//                AllowsBilling = c.AllowsBilling,
		//                AllowsRegistration = c.AllowsRegistration,
		//                AllowsShipping = c.AllowsShipping,
		//                CountryID = c.CountryID,
		//                DisplayOrder = c.DisplayOrder,
		//                Name = c.Name,
		//                NumericISOCode = c.NumericISOCode,
		//                Published = c.Published,
		//                ThreeLetterISOCode = c.ThreeLetterISOCode,
		//                TwoLetterISOCode = c.TwoLetterISOCode
		//            }).FirstOrDefault();
		//}

		#endregion
	}
}
